/*
 * SVMDuplicateDetector.cpp
 *
 *  Created on: 2010-8-4
 *      Author: Chengnian Sun.
 */

#include "SVMDuplicateDetector.h"
#include "../topping/SVMToppingAlgorithm.h"
#include "../util/MacroUtility.h"
#include "../report-model/DuplicateBugReport.h"

void SVMDuplicateDetector::log_detector_summary(FILE* log_file) {
  fprintf(log_file, "SVM Detector\n");
  fprintf(log_file, "--Feature calculator type = %s\n",
      FeatureVectorCalculatorFactory::get_enum_feature_calculator_type_string(
          this->m_feature_calculator_type).c_str());
  fprintf(log_file, "--Evolving model = %s\n",
      this->m_to_evolve_model ? "true" : "false");
}

void SVMDuplicateDetector::post_detection(AbstractBugReport& query_report,
    const vector<const MasterBugReport*>& top) {
  AbstractDuplicateDetector::post_detection(query_report, top);
  if (this->m_to_evolve_model && query_report.is_duplicate()
      && query_report.is_detected()) {

    const DuplicateBugReport& dup =
        *(static_cast<DuplicateBugReport*>(&query_report));
    this->svm_topping_algorithm->add_new_query(dup);
  }
}

void SVMDuplicateDetector::skip() {
  AbstractDuplicateDetector::skip();
  this->svm_topping_algorithm->construct_initial_training_set();
}

AbstractToppingAlgorithm* SVMDuplicateDetector::create_topping_algorithm(
    const ReportBuckets& buckets) {
  SVMToppingAlgorithm* topping_algorithm = new SVMToppingAlgorithm(
      this->get_log_file(), buckets, this->m_feature_calculator_type);
  this->svm_topping_algorithm = topping_algorithm;
  return topping_algorithm;
}

SVMDuplicateDetector::SVMDuplicateDetector(FILE* log_file, unsigned top_number,
    const ReportDataset& report_dataset, const unsigned count_to_skip,
    const vector<IDetectorExtension*>& extensions,
    const enum FeatureVectorCalculatorFactory::EnumFeatureCalculatorType feature_calculator_type,
    const bool to_evolve_model, const bool detecting_all_reports,
    const IndexingType::EnumIndexingType indexing_type) :
    AbstractDuplicateDetector(log_file, top_number, report_dataset,
        count_to_skip, extensions, detecting_all_reports, indexing_type), m_feature_calculator_type(
        feature_calculator_type), m_to_evolve_model(to_evolve_model) {
  this->svm_topping_algorithm = NULL;
}

SVMDuplicateDetector::~SVMDuplicateDetector() {
}
